home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 07 - 1991 / 07.08 Aug 91 / Dots Source / cDotsTask.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-11-02  |  4.6 KB  |  202 lines  |  [TEXT/KAHL]

  1. /***************************************
  2.     cDotsTask.c
  3.         
  4.     SUPERCLASS = CMouseTask
  5.  
  6. Handles mouse clicks in the dots pane.
  7. Also provides undo/redo capability.
  8.  
  9. ****************************************/
  10.  
  11. #include "cDotsTask.h"
  12. #include "dotsTypes.h"
  13.  
  14. /******** C O N S T R U C T I O N ********/
  15.  
  16. /*** IDotsTask
  17. *
  18. * Initialize a DotsTask object
  19. */
  20. void    cDotsTask::IDotsTask(
  21.     short        aNameIndex,
  22.     cDotsPane    *theDotsPane,
  23.     cDotsDoc    *theDotsDoc)
  24. {
  25.     inherited::IMouseTask(aNameIndex);
  26.  
  27.         /* Initialize instance variables */
  28.     fDotsPane = theDotsPane;
  29.     fDotsDoc = theDotsDoc;
  30.     fPlayerMoved = fDotsDoc->getPlayerTurn();    /* Player whose move it is */
  31.     fValidMove = false;    /* Only valid if a previously unselected
  32.                         ** line is selected by this mouse task */
  33.     fDirection = hLine;
  34.     fRow = -1;
  35.     fCol = -1;
  36. }
  37.  
  38. /*****  M O U S E   T R A C K I N G ********/
  39.  
  40. /*** BeginTracking {OVERRIDE}
  41. *
  42. * Mouse tracking starting. Adjust starting pt.
  43. */
  44. void    cDotsTask::BeginTracking(
  45.     Point        *startPt)
  46. {
  47.     
  48.     tLineDir    direction;
  49.     int            row;
  50.     int            col;
  51.     Rect        r;
  52.  
  53.         /* See if pt is on a line */        
  54.     if (fDotsPane->pt2Line(*startPt, &direction, &row, &col)) {
  55.             /* Convert line coords to a pt on the grid */
  56.         fDotsPane->line2Pt(direction, row, col, startPt);
  57.             /* If line unoccupied then fill it */
  58.         if (!fDotsDoc->getLineState(direction, row, col)) {
  59.             fDotsPane->line2Rect(direction, row, col, &r);
  60.             FillRect(&r, black);
  61.         }
  62.     }
  63. }
  64.  
  65.  
  66. /*** KeepTracking {OVERRIDE}
  67. *
  68. * Continuous mouse tracking while the mouse button is down. Draw
  69. * only when the mouse moves or the panorama autoscrolls.
  70. */
  71. void    cDotsTask::KeepTracking(
  72.     Point        *currPt,
  73.     Point        *prevPt,
  74.     Point        *startPt)
  75. {
  76.     
  77.     int            col;
  78.     tLineDir    direction;
  79.     Boolean        eraseIt;
  80.     Rect        r;
  81.     int            row;
  82.  
  83.         /* Only track if not autoScrolling */
  84.     if (!fDotsPane->AutoScroll(*currPt)) {
  85.     
  86.             /* assume won't erase line previously chosen
  87.             ** by this mouse down */
  88.         eraseIt = false;
  89.         
  90.             /* Check for current pt on a line */
  91.         if (fDotsPane->pt2Line(*currPt, &direction, &row, &col)) {
  92.                 /* Convert line coords to a pt on the grid */
  93.             fDotsPane->line2Pt(direction, row, col, currPt);
  94.                 /* Check for new chosen line */
  95.              if (!EqualPt(*currPt, *startPt)) {
  96.                     /* If new chosen line unoccupied then fill it */
  97.                 if (!fDotsDoc->getLineState(direction, row, col)) {
  98.                     fDotsPane->line2Rect(direction, row, col, &r);
  99.                     FillRect(&r, black);
  100.                 }
  101.                 eraseIt = true;    /* erase previous line */
  102.             }
  103.         } else
  104.             eraseIt = true;    /* Erase any previously chosen line */
  105.             
  106.             /* If no longer over a previously chosen line then erase it */
  107.         if (eraseIt) {
  108.                 /* Check if start pt on a line */
  109.             if (fDotsPane->pt2Line(*startPt, &direction, &row, &col)) {
  110.                     /* Only erase if start line is unoccupied */
  111.                 if (!fDotsDoc->getLineState(direction, row, col)) {
  112.                         /* erase the previouly chosen line */
  113.                     fDotsPane->line2Rect(direction, row, col, &r);
  114.                     FillRect(&r, white);
  115.                         /* redraw both dots */
  116.                     fDotsPane->dot2Rect(row, col, &r);
  117.                     FillOval(&r, black);
  118.                     if (direction == hLine)
  119.                         col++;
  120.                     else
  121.                         row++;
  122.                     fDotsPane->dot2Rect(row, col, &r);
  123.                     FillOval(&r, black);
  124.                 }
  125.             }
  126.                 /* Reset startPt to new point */
  127.             SetPt(startPt, currPt->h, currPt->v);
  128.         }
  129.     }
  130. }
  131.  
  132.  
  133. /*** EndTracking {OVERRIDE}
  134. *
  135. * Mouse down tracking has ended because the user has released the
  136. * mouse button. Its now time to complete the move and set up the undo.
  137. */
  138. void    cDotsTask::EndTracking(
  139.     Point        *currPt,
  140.     Point        *prevPt,
  141.     Point        *startPt)
  142. {
  143.     tLineDir    direction;
  144.     int            row;
  145.     int            col;
  146.  
  147.         /* The only valid move is the selection of a previously
  148.         ** unselected line so check if selected point is on line
  149.         ** and the line is unoccupied */ 
  150.     if ((fDotsPane->pt2Line(*prevPt, &direction, &row, &col)) &&
  151.         (!fDotsDoc->getLineState(direction, row, col))) {
  152.         fValidMove = true;
  153.         fDirection = direction;
  154.         fRow = row;
  155.         fCol = col;
  156.         
  157.         Do();    /* Act on users choice */
  158.     }
  159. }
  160.  
  161. /*********** D O   /   U N D O ***********/
  162.  
  163. /*** Do  {OVERRIDE}
  164. *
  165. * Perform a dots pane task, user chose a line
  166. */
  167. void    cDotsTask::Do()
  168. {
  169.     fDotsDoc->setLineState(fDirection, fRow, fCol, fPlayerMoved, true);
  170. }
  171.  
  172. /*** Undo {OVERRIDE}
  173. *
  174. * Undo an action performed by a dots pane task
  175. */
  176. void    cDotsTask::Undo()
  177. {
  178.     fDotsDoc->setLineState(fDirection, fRow, fCol, fPlayerMoved, false);
  179. }
  180.  
  181. /*** Redo {OVERRIDE}
  182. *
  183. * Redo an action performed by a dots pane task
  184. */
  185. void    cDotsTask::Redo()
  186. {
  187.     fDotsDoc->setLineState(fDirection, fRow, fCol, fPlayerMoved, true);
  188. }
  189.  
  190. /*********** A C C E S S ***********/
  191.  
  192. /*** validMove
  193. *
  194. * Return value of fValidMove instance variable.
  195. * The value will be true if a previously unselected line was selected
  196. * during this mouse task, false otherwise.
  197. */
  198. Boolean    cDotsTask::validMove()
  199. {
  200.     return(fValidMove);
  201. }
  202.